package com.example.randommap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baidu.location.BDLocation;
import com.baidu.location.Poi;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.navi.BaiduMapAppNotSupportNaviException;
import com.baidu.mapapi.navi.BaiduMapNavigation;
import com.baidu.mapapi.navi.NaviParaOption;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.utils.DistanceUtil;

import java.util.ArrayList;

public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.AddressHolder> {

    private Context mContext;
    private ArrayList<PoiInfo> list;
    private BDLocation startLocation; // 自己的位置

    public AddressAdapter(Context context, BDLocation bdLocation) {
        mContext = context;
        this.list = new ArrayList<>();
        this.startLocation = bdLocation;
    }

    @NonNull
    @Override
    public AddressHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AddressHolder(LayoutInflater.from(mContext).inflate(R.layout.address_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull AddressHolder holder, int position) {
        //定义起终点坐标
        LatLng startPoint = new LatLng(startLocation.getLatitude(), startLocation.getLongitude());
        LatLng endPoint = list.get(position).getLocation();
        int distance = (int)DistanceUtil.getDistance(startPoint, endPoint);
        String locStr = list.get(position).getName() + ", 距离自己：" + distance + " m";
        holder.textView.setText(locStr);
        holder.textView.setOnClickListener(v -> {
            Toast.makeText(mContext, "点我就跳转到别的APP咯", Toast.LENGTH_SHORT).show();


            //构建导航参数
            NaviParaOption para = new NaviParaOption()
                    .startPoint(startPoint)
                    .endPoint(endPoint)
                    .startName("我的位置")
                    .endName(list.get(position).getName());
            //调起百度地图
            try {
                BaiduMapNavigation.openBaiduMapWalkNavi(para, mContext);
            } catch (BaiduMapAppNotSupportNaviException e) {
                e.printStackTrace();
                //调起失败的处理
            }

            //调起结束时及时调用finish方法以释放相关资源
            BaiduMapNavigation.finish(mContext);
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class AddressHolder extends  RecyclerView.ViewHolder {

        private TextView textView;

        public AddressHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.item_tv);
        }
    }

    public void addItem(PoiInfo poiInfo){
        list.add(0, poiInfo);
        notifyItemInserted(0);
    }

    public void setStartLocation(BDLocation mBDLocation) {
        this.startLocation = mBDLocation;
    }
}
